﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AGRTraining.za.co.agrtraining.model;

namespace AGRTraining.za.co.agrtraining.repository.Impl
{
    public class StudentCourseProgressDAO : IDAO<StudentCourseProgress>
    {
        private AGRModelContainer agrDb = new AGRModelContainer();

        public void persist(StudentCourseProgress entity)
        {
            agrDb.StudentCourseProgresses.AddObject(entity);
        }

        public void remove(StudentCourseProgress entity)
        {
            agrDb.StudentCourseProgresses.DeleteObject(entity);
        }

        public void removeById(string code)
        {
            foreach (StudentCourseProgress progress in agrDb.StudentCourseProgresses.Where(s => s.StudentProgressCode == code))
            {
                agrDb.StudentCourseProgresses.DeleteObject(progress);
            }
        }

        public void merge(StudentCourseProgress entity)
        {
            foreach (StudentCourseProgress progress in agrDb.StudentCourseProgresses.Where(s => s.StudentProgressCode == entity.StudentProgressCode))
            {
                progress.CreditsMaximum = entity.CreditsMaximum;
                progress.CreditsAchieved = entity.CreditsAchieved;
                progress.Competent = entity.Competent;
                progress.CompletionTime = entity.CompletionTime;
            }
        }

        public StudentCourseProgress findById(string code)
        {
            StudentCourseProgress progress = new StudentCourseProgress();

            foreach (StudentCourseProgress prog in agrDb.StudentCourseProgresses.Where(s => s.StudentProgressCode == code))
            {
                progress = prog;
            }

            return progress;
        }

        public IEnumerable<StudentCourseProgress> findAll()
        {
            var progresses = (from prog in agrDb.StudentCourseProgresses
                            select prog);

            return progresses;
        }

        public IEnumerable<StudentCourseProgress> findInRange(int firstResult, int maxResuslts)
        {
            var progresses = (from prog in agrDb.StudentCourseProgresses
                            select prog);

            return progresses.Skip(firstResult).Take(maxResuslts);
        }

        public long count()
        {
            var progresses = (from prog in agrDb.StudentCourseProgresses
                            select prog).ToList();

            return progresses.Count();
        }

        public void commit()
        {
            try
            {
                agrDb.SaveChanges();
            }
            catch
            {

            }
        }
    }
}